﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Reflection;

namespace Baubixinh_client
{
    public partial class frmChonSp : DevExpress.XtraEditors.XtraForm
    {
        public delegate void delegateProductSelected(List<int> lstProductID, List<string> lstProductName, List<string> lstProductEAN, List<double[]> lstGiaBan, List<int> lstNhaSX);
        public event delegateProductSelected evnProductSelected;

        public frmChonSp()
        {
            InitializeComponent();
        }

        private void frmChonSp_Load(object sender, EventArgs e)
        {
            try
            {
                treeDanhMuc.DataSource = Program.gobjDataDanhMuc;
                treeDanhMuc.ExpandAll();
                txtSearch.Focus();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void treeDanhMuc_GetSelectImage(object sender, DevExpress.XtraTreeList.GetSelectImageEventArgs e)
        {
            try
            {
                if (!e.FocusedNode)
                    e.NodeImageIndex = 0;
                else
                    e.NodeImageIndex = 1;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void treeDanhMuc_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                DevExpress.XtraTreeList.Nodes.TreeListNode objNode = treeDanhMuc.FocusedNode;
                if (objNode == null) return;

                //lay cat_id
                //int intCatID = clsCommon.fncCnvNullToInt(objNode.GetValue(colCategory_id_danhmuc));
                string strCatID = xGetRecusiveCatID(objNode);
                strCatID += "-1";

                DataTable objData = clsDbAccess.fncSearchSanPham(Program.gobjDb, strCatID, "", clsCommon.fncCnvNullToInt(rdHienThi.EditValue));
                xAddSelectColumn(objData);
                grdControlSanPham.DataSource = objData;

                objNode.ExpandAll();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private string xGetRecusiveCatID(DevExpress.XtraTreeList.Nodes.TreeListNode objNode)
        {
            string strResult = "";
            try
            {
                int intCatID = clsCommon.fncCnvNullToInt(objNode.GetValue(colCategory_id_danhmuc));

                strResult += intCatID.ToString() + ",";

                for (int i = 0; i <= objNode.Nodes.Count - 1; i++)
                {
                    strResult += xGetRecusiveCatID(objNode.Nodes[i]);
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { }
            return strResult;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                string strSearch = txtSearch.Text.Trim();

                if (clsCommon.fncIsBlank(strSearch)) return;

                DataTable objData = clsDbAccess.fncSearchSanPham(Program.gobjDb, "", strSearch, clsCommon.fncCnvNullToInt(rdHienThi.EditValue));
                xAddSelectColumn(objData);
                grdControlSanPham.DataSource = objData;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private bool xAddSelectColumn(DataTable objData)
        {
            bool blnResult = false;
            try
            {
                if (objData == null) return false;
                objData.Columns.Add("chon", typeof(bool));

                blnResult = true;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { }
            return blnResult;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnChon_Click(object sender, EventArgs e)
        {
            try
            {
                if (!clsCommon.fncMsgConfirm("Bạn có chắc muốn chọn sản phẩm này?")) return;

                Cursor.Current = Cursors.WaitCursor;

                List<int> lstProductID = new List<int>();
                List<string> lstProductName = new List<string>();
                List<string> lstProductEAN = new List<string>();
                List<double[]> lstGiaBan = new List<double[]>();
                List<int> lstNhaSX = new List<int>();

                int intProID;
                string strProName;
                string strProEAN;
                bool blnSelected;
                double[] dblGiaBan;
                int intNhaSX;

                //doc id va name
                for (int i = dgvSanPham.RowCount - 1; i >= 0; i--)
                {
                    blnSelected = clsCommon.fncCnvNullToBool(dgvSanPham.GetRowCellValue(i, colChon));
                    if (!blnSelected) continue;

                    intProID = clsCommon.fncCnvNullToInt(dgvSanPham.GetRowCellValue(i, colProduct_id));
                    strProName = clsCommon.fncCnvNullToString(dgvSanPham.GetRowCellValue(i, colPro_name));
                    strProEAN = clsCommon.fncCnvNullToString(dgvSanPham.GetRowCellValue(i, colProduct_ean));

                    dblGiaBan = new double[6];
                    dblGiaBan[(int)clsEnum.GIA_BAN_INDEX.GIA_GOC] = clsCommon.fncCnvNullToDbl(dgvSanPham.GetRowCellValue(i, colGia_goc));
                    dblGiaBan[(int)clsEnum.GIA_BAN_INDEX.GIA_LE] = clsCommon.fncCnvNullToDbl(dgvSanPham.GetRowCellValue(i, colGia_ban_le));
                    dblGiaBan[(int)clsEnum.GIA_BAN_INDEX.GIA_CAP1] = clsCommon.fncCnvNullToDbl(dgvSanPham.GetRowCellValue(i, colGia_cap1));
                    dblGiaBan[(int)clsEnum.GIA_BAN_INDEX.GIA_CAP2] = clsCommon.fncCnvNullToDbl(dgvSanPham.GetRowCellValue(i, colGia_cap2));
                    dblGiaBan[(int)clsEnum.GIA_BAN_INDEX.GIA_CAP3] = clsCommon.fncCnvNullToDbl(dgvSanPham.GetRowCellValue(i, colGia_cap3));
                    dblGiaBan[(int)clsEnum.GIA_BAN_INDEX.KHUYEN_MAI] = clsCommon.fncCnvNullToDbl(dgvSanPham.GetRowCellValue(i, colKhuyenMai));

                    intNhaSX = clsCommon.fncCnvNullToInt(dgvSanPham.GetRowCellValue(i, colProduct_manufacturer_id));

                    lstProductID.Add(intProID);
                    lstProductName.Add(strProName);
                    lstProductEAN.Add(strProEAN);
                    lstGiaBan.Add(dblGiaBan);
                    lstNhaSX.Add(intNhaSX);

                    dgvSanPham.DeleteRow(i);
                }

                //phat ra su kien
                evnProductSelected(lstProductID, lstProductName, lstProductEAN, lstGiaBan, lstNhaSX);
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void btnAddProduct_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                frmAddProduct frmAdd = new frmAddProduct();
                frmAdd.ShowDialog();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void dgvSanPham_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            try
            {
                if (e.Column == colProduct_thumb_image && e.IsGetData)
                {
                    string fileName = clsCommon.fncCnvNullToString(dgvSanPham.GetRowCellValue(e.ListSourceRowIndex, colThumb));

                    Image img = null;
                    img = Image.FromFile(fileName);

                    e.Value = img;

                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

    }
}